package com.rykj.qxj.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.rykj.qxj.annotation.EnableSystemLog;
import com.rykj.qxj.server.common.exception.ApiAbilityStaticsticsIsNullException;
import com.rykj.qxj.server.common.exception.AppIdIsNullException;
import com.rykj.qxj.server.common.exception.ApplyIsNotExistException;
import com.rykj.qxj.server.model.entity.ApiWarnInfo;
import com.rykj.qxj.server.services.IApiWarnInfoService;
import com.rykj.qxj.util.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;

@Log4j2
@RestController
@RequestMapping("/v1/warnInfo")
@Api(tags="应用预警信息管理")
public class ApiWarnInfoController {
	@Autowired
	private IApiWarnInfoService apiWarnInfoService;
	@PostMapping("/saveWarnInfo")
    @ApiOperation(value="保存应用预警信息",notes ="保存应用预警信息")
    @EnableSystemLog(value="保存应用预警信息",fullPath ="/v1/warnInfo/saveWarnInfo")
    public Result saveApiWarnInfo(@RequestBody ApiWarnInfo apiWarnInfo){
        	try {
				apiWarnInfoService.saveApiWarnInfo(apiWarnInfo);
			} catch (ApplyIsNotExistException e) {
				e.printStackTrace();
				return new Result(false, e.getMessage());
			} catch (AppIdIsNullException e) {
				e.printStackTrace();
				return new Result(false, e.getMessage());
			} catch (ApiAbilityStaticsticsIsNullException e) {
				e.printStackTrace();
				return new Result(false, e.getMessage());
			}
            return new Result(true,"保存成功");
    }
	@GetMapping("/list")
	@ApiOperation(value="应用接口的预警报表",notes ="应用接口的预警报表")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
        @ApiImplicitParam(name = "appKey",value ="appKey",required = true,paramType = "query")
})
	public Result selectPageApiWarnInfoByappId(@RequestParam(value = "pageNum") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize,
            @RequestParam(value="appKey") String appKey) {
		if(StringUtil.isEmpty(appKey)) {
			return new Result(false, "查看应用的预警报表appKey不能为空");
		}
	   try{
		  PageInfo page = apiWarnInfoService.selectPageApiWarnInfoByappKey(pageNum, pageSize, appKey);
	          return new Result(page);
	       }catch (Exception e){
	            log.error("查询失败", e);
	            return new Result(false, "查询失败");
	      }
	}
	
	

}
